#include "lora_thread.h"
#include "main.h"
#include "spi.h"
#include "py32f0xx_hal_gpio.h"
#include "py32f0xx_HAL_spi.h"
#include "radio.h"	
#include "Model_Info.h"
#include "malloc.h"
#include "JQueue.h"
#include "Switch.h"
lora_t lora_sx126;

JQueue_t  lora_send_mq;         //lora发送消息队列
#define  lora_send_mq_size 10
uint8_t lora_send_buf[sizeof(LoRaData_t)*lora_send_mq_size]={0};

extern Mallco_dev mallco_dev;         //内存管理器
  
uint8_t tx_model=0;

void Lora_Recive(uint8_t *data,uint8_t len);
void Lora_Relay(LoRa_LLP_Hand_t hand,uint8_t *data,uint8_t len);    //中继
void Replay_LoRaSend_Data(LoRa_LLP_Hand_t hand,uint8_t *data,uint8_t len);  //中继发送数据


void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin==RA_DIO1_Pin)
	{
		if(tx_model==1)
		{
			//rt_sem_release(Dio1_EXIT_sem);
		}
	}
}
/************************************
 * 功能：GPIO 端口函数及延时函数
 * 参数：
 * 返回：
 * *********************************/
uint32_t nf_gpio_and_delay(lora_t *nf,uint8_t msg, uint8_t arg_int,void *arg_ptr,void *res_arg_ptr)
{
	uint32_t status=0;
	//uint32_t udelay= *(uint32_t *)arg_ptr;
	switch (msg)
	{
		case LoRa_MSG_DELAY_MILLI:
			HAL_Delay(arg_int);
			//rt_thread_mdelay(arg_int);
			break;
		case LoRa_MSG_GPIO_CS:              //CS引脚
			if(arg_int)
			{
				HAL_GPIO_WritePin(RA_CSN_GPIO_Port,RA_CSN_Pin,GPIO_PIN_SET);
			}
			else
			{
				HAL_GPIO_WritePin(RA_CSN_GPIO_Port,RA_CSN_Pin,GPIO_PIN_RESET);
			}
			break;
		case LoRa_MSG_GPIO_RST:
			if(arg_int)
			{
				HAL_GPIO_WritePin(RA_RST_GPIO_Port,RA_RST_Pin,GPIO_PIN_SET);
			}
			else
			{
				HAL_GPIO_WritePin(RA_RST_GPIO_Port,RA_RST_Pin,GPIO_PIN_RESET);
			}
			break;
		case LoRa_MSG_GPIO_ReadDio0:    //读取DIO0 状态
			//status=HAL_GPIO_ReadPin(RA_DIO0_GPIO_Port,RA_DIO0_Pin);
		  break;
		case LoRa_MSG_GPIO_ReadDio1:   //读取DIO1 状态
			status=HAL_GPIO_ReadPin(RA_DIO1_GPIO_Port,RA_DIO1_Pin);
		  break;
		case LoRa_MSG_GPIO_BUSY:
			status=HAL_GPIO_ReadPin(RA_BUSY_GPIO_Port,RA_BUSY_Pin);
		  break;
		case LoRa_MSG_GetTick:        //获取系统时间
			  //status=HAL_GetTick();
		  break;
		case LoRa_MSG_GPIO_EXIT_DIO1:  //等待DIO1 中断
		   //tx_model=1;
			   //status=rt_sem_take(Dio1_EXIT_sem, udelay);
		     //tx_model=0;
			   break;
		default:
			break;
	}
	return status;
}
/*******************************************
 * 功能：spi通信函数
 * 参数：
 * 返回：
 * ****************************************/
uint32_t  lora_4wire_hw_spi(lora_t *nf, uint8_t msg, uint8_t arg_int,void *arg_ptr,void *res_arg_ptr)
{
	switch(msg)
	{

		case LoRa_MSG_BYTE_START_TRANSFER:
			break;
		case LoRa_MSG_BYTE_END_TRANSFER:
			break;
		case LoRa_MSG_BYTE_INIT:
			break;
		case LoRa_MSG_BYTE_Transmit:
			HAL_SPI_Transmit(&hspi1,(uint8_t *)arg_ptr,arg_int,1000);
		    break;
		case LoRa_MSG_BYTE_Receive:
		    HAL_SPI_Receive(&hspi1,(uint8_t *)arg_ptr,arg_int,1000);
		    break;
		default:
			return 0;
	}
	return 1;
}
//#define TX_MOD 1
lora_config_t localSettingSave = {435000000, 20, 7, 7, 1, 0x000f}; // 本地保存的配置信息
uint8_t rx_buf[128];
uint8_t rx_len=128;

void Lora_Init()
{
	Q_ERR err;
  uint32_t rf=0;
	J_Create_Queue(&lora_send_mq,lora_send_mq_size,lora_send_buf,sizeof(LoRaData_t),JQ_NO_LOCK_FIFO_QUEUE,&err);   //创建队列
	Lora_Setup_SX126x_noname_f(&lora_sx126,lora_4wire_hw_spi,nf_gpio_and_delay);
	
  rf=420000000+(model_info_handle.general_info.SN%30)*1000000;															
	localSettingSave.Power=model_info_handle.model_info.rf_config.RF_power;
	localSettingSave.RFFrequency=rf;//model_info_handle.model_info.rf_config.RF_TXFrequency;
	localSettingSave.SignalBw=model_info_handle.model_info.rf_config.SignalBw;
	localSettingSave.SpreadingFactor=model_info_handle.model_info.rf_config.SpreadingFactor;
	localSettingSave.ErrorCoding=model_info_handle.model_info.rf_config.ErrorCoding;
																
  sx126xLoraInit(&lora_sx126, &localSettingSave);
  sx126xStartRx(&lora_sx126,model_info_handle.model_info.rf_config.RF_RXFrequency);	
}
/// @brief lora 任务入口函数
/// @param paremeter 
void lora_loop(uint32_t ms)
{
	Q_ERR err;
	uint8_t rx_status=0;
  LoRaData_t LoraSend_data;
	if(tx_model==1)
	{
		if(sx126xWaitSend(&lora_sx126,ms)!=1)
		{
			tx_model=0;
			sx126xStartRx(&lora_sx126,model_info_handle.model_info.rf_config.RF_RXFrequency);
		  //myfree(&mallco_dev,LoraSend_data.data);
		}
	}
	else
	{
		J_Dequeue(&lora_send_mq,&LoraSend_data,&err);
		if(err==Q_ERR_None)
	  {
		  sx126xStartSend(&lora_sx126,model_info_handle.model_info.rf_config.RF_TXFrequency,LoraSend_data.data,LoraSend_data.len,2000);
		  myfree(&mallco_dev,LoraSend_data.data);
			tx_model=1;
	  }
		else
		{
			rx_len=128;
	    rx_status=sx126xRxData(&lora_sx126,rx_buf,&rx_len);
	    if(rx_status==0)
	    {
		    Lora_Recive(rx_buf,rx_len);
	    }
		}
	}
}
/******************************
******************************/
void Lora_Recive(uint8_t *data,uint8_t len)
{
	Lora_Switch_Recive(data,len);
}
/********************
 * 功能：用户通过Lora发送数据
 * 参数：
********************/
void User_LoRaSend_Data(uint8_t *data,uint8_t len)
{
	Q_ERR err;
	LoRaData_t lora_data;  
	lora_data.len=len;
  lora_data.data=mymalloc(&mallco_dev,len);
	if(lora_data.data)
	{
		mymemcpy(lora_data.data,data,len);    //复制包头
		J_Enqueue(&lora_send_mq,&lora_data,&err);
	  if(err!=Q_ERR_None)
		{
			myfree(&mallco_dev,lora_data.data);
		}
	}
}
